package com.hotel.front.controller.person;

import com.hotel.comm.bean.DubboReply;
import com.hotel.comm.bean.IReply;
import com.hotel.comm.bean.person.EjobUser;
import com.hotel.comm.service.person.IPersonManagerService;
import com.hotel.front.bean.Constants;
import com.hotel.front.bean.FieldValidatorResponse;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping(value = "/person")
public class PersonLoginController {
    @Autowired
    private IPersonManagerService personManagerService;
/*    @Autowired
    private ILuceneWriterService luceneWriterService;*/

    private static final Logger LOGGER = LoggerFactory.getLogger(PersonLoginController.class);

    /**
     * 用户登录
     *
     * @param ejobUser
     * @param request
     * @return
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(EjobUser ejobUser,
                        HttpServletRequest request) {
        IReply reply = personManagerService.findUserByLoginName(ejobUser.getLoginName());
        if (reply != null && reply.isSuccess()) {
            EjobUser user = (EjobUser) ((DubboReply) reply).getT();
            if (user != null && StringUtils.equals(user.getPassword(), DigestUtils.md5Hex(ejobUser.getPassword()))) {
                request.getSession().setAttribute(Constants.LOGIN_USER, user);
            }
        }
        return Constants.INDEX;
    }

    /**
     * 用户注册时检查用户名是否存在
     *
     * @param loginName
     * @return
     */
    @RequestMapping(value = "/checkUserExist")
    @ResponseBody
    public FieldValidatorResponse checkRegisterUserName(@RequestParam(value = "loginName") String loginName) {
        FieldValidatorResponse remoteFieldValidatorResponse = new FieldValidatorResponse();
        remoteFieldValidatorResponse.setValid(true);
        if (StringUtils.isNotBlank(loginName)) {
            IReply reply = personManagerService.findUserByLoginName(loginName);
            if (reply != null && reply.isSuccess()) {
                EjobUser user = (EjobUser) ((DubboReply) reply).getT();
                if (user != null) {
                    remoteFieldValidatorResponse.setValid(false);
                }
            }
        }
        return remoteFieldValidatorResponse;
    }

    /**
     * 用户登录时检查用户是否存在
     *
     * @param loginName
     * @return
     */
    @RequestMapping(value = "/checkLoginUserExist")
    @ResponseBody
    public FieldValidatorResponse checkLoginUserExist(@RequestParam(value = "loginName") String loginName) {
        FieldValidatorResponse remoteFieldValidatorResponse = new FieldValidatorResponse();
        remoteFieldValidatorResponse.setValid(false);
        if (StringUtils.isNotBlank(loginName)) {
            IReply reply = personManagerService.findUserByLoginName(loginName);
            if (reply != null && reply.isSuccess()) {
                EjobUser user = (EjobUser) ((DubboReply) reply).getT();
                if (user != null) {
                    remoteFieldValidatorResponse.setValid(true);
                }
            }
        }
        return remoteFieldValidatorResponse;
    }

    /**
     * 用户登录时检查密码是否正确
     *
     * @param ejobUser
     * @return
     */
    @RequestMapping(value = "/checkLoginUserPasswordValid")
    @ResponseBody
    public FieldValidatorResponse checkLoginUserPasswordValid(EjobUser ejobUser) {
        FieldValidatorResponse remoteFieldValidatorResponse = new FieldValidatorResponse();
        remoteFieldValidatorResponse.setValid(false);
        if (StringUtils.isNotBlank(ejobUser.getLoginName())) {
            IReply reply = personManagerService.findUserByLoginName(ejobUser.getLoginName());
            if (reply != null && reply.isSuccess()) {
                EjobUser user = (EjobUser) ((DubboReply) reply).getT();
                if (user != null && StringUtils.equals(user.getPassword(), DigestUtils.md5Hex(ejobUser.getPassword()))) {
                    remoteFieldValidatorResponse.setValid(true);
                }
            }
        }
        return remoteFieldValidatorResponse;
    }

    /**
     * 创建用户
     *
     * @param ejobUser
     * @param randomCode
     * @param request
     * @return
     */
    @RequestMapping(value = "/create_user", method = RequestMethod.POST)
    public String createCompanyUser(EjobUser ejobUser, @RequestParam(value = "randomCode", defaultValue = "") String randomCode,
                                    HttpServletRequest request) {
        LOGGER.info("loginName:" + ejobUser.getLoginName() + ",password:" + ejobUser.getPassword() + "randomCode:" + randomCode);
        if (StringUtils.isNotBlank(ejobUser.getLoginName()) && StringUtils.isNotBlank(ejobUser.getPassword())) {
            String randomCodeStore = (String) request.getSession().getAttribute(Constants.RANDOM_IN_SESSION);

            //验证码验证
            if (StringUtils.equals(randomCode, randomCodeStore)) {
//                ejobUser.setPassword(DigestUtils.md5Hex(ejobUser.getPassword()));
                DubboReply reply = (DubboReply) personManagerService.regist(ejobUser.getLoginName(), ejobUser.getPassword());
                if (reply != null && reply.isSuccess()) {
                    request.getSession().setAttribute(Constants.LOGIN_USER, ejobUser);
                } else {
                    LOGGER.info(reply.getReplyMsg());
                }
            }
        }
        return Constants.INDEX;
    }
}
